package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.GroupMessageEntity;
import com.sinothk.gpserver.modules.biz.service.IGroupMessageService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  1. 群消息功能
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-09-10
 */
@Api(tags = "群消息功能")
@RequestMapping("/groupMessage")
@ResponseResult()
@RestController
public class GroupMessageController extends BaseController {

    @Resource
    private IGroupMessageService groupMessageService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<GroupMessageEntity> page(@RequestBody PageVo<GroupMessageEntity> pageVo) {
        if (pageVo == null) throw new BizException("分页参数pageVo不能为空");
        if (pageVo.getData() == null) throw new BizException("分页参数Data不能为空");
        if (pageVo.getData().getGroupId() == null) throw new BizException("分页参数groupId不能为空");
        return groupMessageService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public GroupMessageEntity get(@ApiParam("id") @PathVariable String id) {
        return groupMessageService.getById(id);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public String add(@RequestBody GroupMessageEntity groupMessageEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        groupMessageEntity.setCreateBy(login.getAccount());
        groupMessageService.save(groupMessageEntity);
        return "新增成功";
    }


    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public GroupMessageEntity update(@RequestBody GroupMessageEntity groupMessageEntity) {
        if (StringUtil.isEmpty(groupMessageEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        groupMessageEntity.setUpdateBy(login.getUserName());
        groupMessageService.updateById(groupMessageEntity);
        return groupMessageService.getById(groupMessageEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody GroupMessageEntity groupMessageEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(groupMessageEntity.getId())) {
            groupMessageEntity.setCreateBy(login.getAccount());
            groupMessageService.save(groupMessageEntity);
            return "新增成功";
        } else {
            groupMessageEntity.setUpdateBy(login.getUserName());
            groupMessageService.updateById(groupMessageEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        groupMessageService.removeById(id);
        return "已逻辑删除";
    }
}
